package com.itheima.dao;


import com.itheima.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {

    @Select("select * from users where username=#{username}")
    @Results(id = "userMap",value={
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,
                    many=@Many(select = "com.itheima.dao.RoleDao.findById")
            ),
    })
    UserInfo findByUsername(String username) throws Exception;

    /*查询用户信息*/
    @Select("select * from users")
    List<UserInfo> findAll() throws Exception;

    /*添加用户*/
    @Insert("insert into users(username,email,password,phoneNum,status) values (#{username},#{email},#{password},#{phoneNum},#{status})")
    void save(UserInfo user) throws  Exception;

    /*根据id查询用户*/
    @Select("select * from users where id = #{id}")
    @ResultMap("userMap")
    UserInfo findById(String id) throws Exception;

    //添加用户和角色的关系
    @Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId})")
    void addRoleToUser(@Param("userId") String userId,@Param("roleId") String roleId)throws Exception;
}
